<?php

namespace app\api\controller\v1;


use app\admin\model\Material;

use app\admin\model\Materialimage;
use app\admin\model\Porder;
use app\admin\model\Surface;
use app\admin\model\Texture;
use think\Cache;

class Order extends Base
{
    //列表
    public function orderList()
    {
        $page                       = $this->params['page'] ?? 1;

        $map                        = [];
        $ordertype                  = !empty($this->params['ordertype']) ? $this->params['ordertype'] : [];//1现货 2定制
        $orderstatus                = !empty($this->params['orderstatus']) && $this->params['orderstatus'] !== 0  ? $this->params['orderstatus'] : [];//1=待确认；2=待发货；3=已发货；4=已签收；5=待评价；6=售后'
        $keyword                    = !empty($this->params['keyword']) ? $this->params['keyword'] : [];
        $time                       = !empty($this->params['time']) ? $this->params['time'] : [];
        $time_type                  = !empty($this->params['time_type']) ? $this->params['time_type'] : [];

        $cacheTag       = \config('cache_tag')['v1_order_orderList'];

        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . $page . json_encode($ordertype) . json_encode($orderstatus) . json_encode($keyword) . json_encode($time) . json_encode($time_type);
        $data           =  Cache::get($cacheKey);


        if(!$data)
        {
            //组装查询条件
            if(!empty($ordertype))
            {
//            $map['fk_bdl_ordertype'] = $ordertype;
            }

            if(!empty($orderstatus))
            {
                $map['fk_bdl_orderstatus'] = ['=', $orderstatus];
            }


            if(!empty($keyword))
            {
                $map['fk_bdl_erpbillno'] = ['like',"%{$keyword}%"];
            }

            if(!empty($time) && $time_type == 5)
            {
                $time = explode('~',$time);
                $map['fk_bdl_ordertime'] = ['between time',[ $time[0] . " 00:00:00", $time[1] . " 23:59:59"]];
            }else{
                if(in_array($time_type,[1,2,3,4]) && !empty($time_type))
                {
                    $time = $this->getStartEnd($time_type);
                    $map['fk_bdl_ordertime'] = ['between time',[$time['start'],$time['end']]];
                }

            }

            //结束

            $data                             = Porder::where($map)
                ->where('fk_bdl_bd_customer',$this->customer_id)
                ->order('fk_bdl_erpbillno','desc')
                ->where('fk_bdl_bd_datacenter',$this->datacenter)
                ->whereIn('forgid',$this->forgids)
                ->page($page,$this->pageSize)
                ->select();

            //前端返回数据
            $new_data = [];
            foreach ($data as $k => $v)
            {
                $new = [
                    'fk_bdl_erpbillno'              => $v['fk_bdl_erpbillno'],
                    'fk_bdl_allqty'                 => $v['fk_bdl_allqty'],
                    'fk_bdl_allamount'              => $v['fk_bdl_allamount'],
                    'fk_bdl_ordertime'              => $v['fk_bdl_ordertime'],
                    'fk_bdl_orderstatus'            => $v['fk_bdl_orderstatus'],
                    'fk_bdl_ordertype'              => 1,


                    'fk_bdl_planshiptime'               => $v['fk_bdl_planshiptime'],
                    'fk_bdl_signtime'                   => $v['fk_bdl_signtime'],
                    'fk_bdl_planreceivetime'            => $v['fk_bdl_planreceivetime'],
                    'id'                                => $v['id'],
                    'look_time'                         => $v['fk_bdl_ordertime'],


                    //
                    'fk_bdl_bizpersonlevel'            => $v['fk_bdl_bizpersonlevel'],
                    'fk_bdl_carinfolevel'            => $v['fk_bdl_carinfolevel'],
                    'fk_bdl_evaluateinfo'            => $v['fk_bdl_evaluateinfo'],

                ];

                $son = [];
                $dejson_data = json_decode($v['fk_bdl_orderjsondata'], true);

                if(!empty($dejson_data))
                {
                    foreach ($dejson_data as $kk => $vv)
                    {

                        $img   = Material::where('fname',$vv['bdl_bd_material_name'])
                            ->whereIn('forgid',$this->forgids)
                            ->where('fk_bdl_bd_datacenter',$this->datacenter)
                            ->find();

                        //拿到表面

                        $face  = Surface::whereIn('forgid',$this->forgids)->where('fk_bdl_bd_datacenter',$this->datacenter)->where('fnumber',$vv['bdl_bd_surface_number'])
                            ->find();
                        if(!empty($face))
                        {
                            $surface_id     = $face['id'];
                            $material_id    = $img['id'];
                            if(!empty($surface_id) && !empty($material_id))
                            {
                                $find       = Materialimage::where('fk_bdl_bd_surface',$surface_id)->where('fk_bdl_bd_material',$material_id)->find();
                                if(!empty($find))
                                {
                                    $show_image = $find['fk_bdl_imageurl'];
                                }else{
                                    $show_image = $this->getDefaultImage();
                                }
                            }else{
                                $show_image = $this->getDefaultImage();
                            }

                        }
                        $son[] = [
                            'texture'           => $vv['bdl_bd_texture_name'],
                            'surface'           => $vv['bdl_bd_surface_name'],
                            'bdl_thickness'     => floatval($vv['bdl_thickness']),
                            'bdl_width'         => floatval($vv['bdl_width']),
                            'bdl_length'        => floatval($vv['bdl_length']),
                            'bdl_refthickness'  => floatval($vv['bdl_refthickness']),
                            'bdl_materialgrade' => !empty($vv['bdl_bd_material_grade']) ? $vv['bdl_bd_material_grade'] : '',

                            'bdl_unitid_name'   => !empty($vv['bdl_bd_material_bdl_unitid_name']) ? $vv['bdl_bd_material_bdl_unitid_name'] : '',
                            'bdl_amount'        => !empty($vv['bdl_salamt']) ? handle_money($vv['bdl_salamt']) : '',
                            'bdl_qty'           => !empty($vv['bdl_qty']) ? $vv['bdl_qty'] : '',
                            'url'               => !empty($show_image) ? $show_image : $this->getDefaultImage()
                        ];
                    }
                }


                $new['son'] = $son;

                $new_data[] = $new;
            }

            $data = $new_data;

            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);
        }

        $this->success('',$data);
    }


    public function orderDetail()
    {
        $check      = $this->plulicValidate($this->params,
            ['id|ID'],
            ['require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        $cacheTag       = \config('cache_tag')['v1_order_orderList'];
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . $this->params['id'];
        $data           =  Cache::get($cacheKey);

        if(!$data)
        {
            $datadata = Porder::with(['carinfo','address'])->where('id',$this->params['id'])->find();


            $new = [
                'fk_bdl_erpbillno'              => $datadata['fk_bdl_erpbillno'],
                'fk_bdl_allqty'                 => $datadata['fk_bdl_allqty'],
                'fk_bdl_allamount'              => $datadata['fk_bdl_allamount'],
                'fk_bdl_orderstatus'            => $datadata['fk_bdl_orderstatus'],
                'fk_bdl_ordertype'              => 1,


//            'fk_bdl_planshiptime'               => $datadata['fk_bdl_planshiptime'],
//            'fk_bdl_signtime'                   => $datadata['fk_bdl_signtime'],
//            'fk_bdl_planreceivetime'            => $datadata['fk_bdl_planreceivetime'],
                'id'                                => $datadata['id'],
                'carinfo'                           => $datadata['carinfo'],
                'address'                           => $datadata['address'],


                'fk_bdl_ordertime'                  => $datadata['fk_bdl_ordertime'],
                'fk_bdl_confirmtime'                => $datadata['fk_bdl_confirmtime'],
                'fk_bdl_planshiptime'               => $datadata['fk_bdl_planshiptime'],
                'fk_bdl_signtime'                   => $datadata['fk_bdl_signtime'],
                'fk_bdl_planreceivetime'            => $datadata['fk_bdl_planreceivetime'],
                'fk_bdl_signattach'                 => $datadata['fk_bdl_signattach'],
                'fk_bdl_materialbill'               => $datadata['fk_bdl_materialbill'],
                'fk_bdl_bizpersonlevel'             => $datadata['fk_bdl_bizpersonlevel'],
                'fk_bdl_carinfolevel'               => $datadata['fk_bdl_carinfolevel'],
                'fk_bdl_evaluateinfo'               => $datadata['fk_bdl_evaluateinfo'],

            ];

            $son = [];
            $dejson_data = json_decode($datadata['fk_bdl_orderjsondata'], true);



            if(!empty($dejson_data))
            {
                foreach ($dejson_data as $kk => $vv)
                {
                    $img   = Material::where('fname',$vv['bdl_bd_material_name'])
                        ->whereIn('forgid',$this->forgids)
                        ->find();


                    //拿到表面
                    $face  = Surface::whereIn('forgid',$this->forgids)->where('fnumber',$vv['bdl_bd_surface_number'])
                        ->find();
                    if(!empty($face))
                    {
                        $surface_id     = $face['id'];
                        $material_id    = $img['id'];
                        if(!empty($surface_id) && !empty($material_id))
                        {
                            $find       = Materialimage::where('fk_bdl_bd_surface',$surface_id)->where('fk_bdl_bd_material',$material_id)->find();
                            if(!empty($find))
                            {
                                $show_image = $find['fk_bdl_imageurl'];
                            }else{
                                $show_image = $this->getDefaultImage();
                            }
                        }else{
                            $show_image = $this->getDefaultImage();
                        }

                    }

                    $son[] = [
                        'texture'           => !empty($vv['bdl_bd_texture_name']) ? $vv['bdl_bd_texture_name'] : '',
                        'surface'           => !empty($vv['bdl_bd_surface_name']) ? $vv['bdl_bd_surface_name'] : '',
                        'bdl_thickness'     => !empty($vv['bdl_thickness']) ? floatval($vv['bdl_thickness']) : '',
                        'bdl_width'         => !empty($vv['bdl_width']) ? floatval($vv['bdl_width']) : '',
                        'bdl_length'        => !empty($vv['bdl_length']) ? floatval($vv['bdl_length']) : '',
                        'bdl_refthickness'  => !empty($vv['bdl_refthickness']) ? floatval($vv['bdl_refthickness']) : '',
                        'bdl_unitid_name'   => !empty($vv['bdl_bd_material_bdl_unitid_name']) ? $vv['bdl_bd_material_bdl_unitid_name'] : '',
                        'bdl_amount'        => !empty($vv['bdl_salamt']) ? handle_money($vv['bdl_salamt']) : '',
                        'bdl_materialgrade' => !empty($vv['bdl_bd_material_grade']) ? $vv['bdl_bd_material_grade'] : '',
                        'bdl_qty'           => !empty($vv['bdl_qty']) ? $vv['bdl_qty'] : '',
                        'url'               => !empty($show_image) ? $show_image : $this->getDefaultImage()
                    ];
                }
            }

            $new['son'] = $son;

            $data = $new;


            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);

        }



        $this->success('',$data);

    }


    //评价
    public function commentOrder()
    {
        $check      = $this->plulicValidate($this->params,
            ['id|ID','fk_bdl_bizpersonlevel|业务员评价等级','fk_bdl_carinfolevel|配送车辆评价等级'],
            ['require','require','require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        $info = Porder::get($this->params['id']);

        if(!$info){
            $this->error('信息错误');
        }

        $data = [
            'fk_bdl_bizpersonlevel'     => $this->params['fk_bdl_bizpersonlevel'],
            'fk_bdl_carinfolevel'       => $this->params['fk_bdl_carinfolevel'],
            'fk_bdl_evaluateinfo'       => !empty($this->params['fk_bdl_evaluateinfo']) ? $this->params['fk_bdl_evaluateinfo'] : ' ',
//            'fk_bdl_orderstatus'        => 5
        ];

        $res = $info->save($data);

        if(!$res)
        {
            $this->error('评价失败');
        }

        $cacheTag       = \config('cache_tag')['v1_order_orderList'];
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . $this->params['id'];
        Cache::clear($cacheKey);

        $this->success('评价成功');
    }
}